package com.kyub.spring.gears.introspect;

import com.kyub.spring.gears.model.PojoRegistry;
import org.springframework.util.StringUtils;

import javax.naming.ConfigurationException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * Ancestro fro pojo inspection factories
 */
public abstract class MetadataClassFactory {

    protected static final List<String> fieldsBlackList = new ArrayList<String>();
    protected static final List<Class> simpleTypes = new ArrayList<Class>();
    protected static final List<Class> collectionTypes = new ArrayList<Class>();

    protected PojoRegistry registry;


    static {
        simpleTypes.add(Integer.class);
        simpleTypes.add(String.class);
        simpleTypes.add(Long.class);
        simpleTypes.add(Double.class);
        simpleTypes.add(Date.class);
        simpleTypes.add(Boolean.class);

        collectionTypes.add(Collection.class);
        collectionTypes.add(Set.class);
        collectionTypes.add(List.class);

        fieldsBlackList.add("class");

    }

    public MetadataClassFactory(PojoRegistry registry) {
        this.registry = registry;
    }

    protected String checkIdProperty(final String basePojoName, final Method[] methods) throws ConfigurationException {
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equalsIgnoreCase("getId")
                    || methods[i].getName().equalsIgnoreCase("get" + basePojoName + "Id")) {
                return StringUtils.uncapitalize(methods[i].getName().substring(3));
            }
        }
        throw new ConfigurationException("No suitable id found in autocofig for " + basePojoName);

    }

}
